package main

import (
	"bytes"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"strings"
)

func main() {

	// x-www-form-urlencoded 格式提交数据请求

	values := url.Values{}
	values.Add("x", "1")
	values.Add("x", "2")
	values.Set("y", "10")
	values.Set("y", "20")

	response, err := http.PostForm("http://127.0.0.1:9999", values)
	if err != nil {
		fmt.Println(err)
	}
	// fmt.Fprintf()

	io.Copy(os.Stdout, response.Body)

	reader := strings.NewReader(`
	{
		"a":1,
		"b":2
	}
	`)
	response, _ = http.Post("http://127.0.0.1:9999", "application/json", reader)
	io.Copy(os.Stdout, response.Body)

	buffer := bytes.NewBuffer(nil)
	writer := multipart.NewWriter(buffer)

	filewriter, _ := writer.CreateFormFile("file", "a.json")
	f, _ := os.Open("a.json")
	io.Copy(filewriter, f)
	writer.Close()
	response, _ = http.Post("http://127.0.0.1:9999", "multipart/form-data", buffer)
	io.Copy(os.Stdout, response.Body)

}
